﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows;
using Coding4Fun.Kinect.Wpf.Controls;
using System.Windows.Media;
using System.Windows.Input;

namespace Hugo
{
    class HandCursor
    {
        public HoverButton hand;
        private Canvas canvas;

        public const int handSize = 75;

        void cursorClicked(object sender, RoutedEventArgs e)
        {
            MouseButtonEventArgs me = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left, null);
            me.RoutedEvent = Mouse.MouseUpEvent;
            MainWindow.getInstance().respondToClick(me);
        }

        private bool isHov = false;

        public void setHovering(bool b)
        {
            if (b) { hand.Hovering(); }
            else { hand.Release(); }
            isHov = b;
        }

        public bool isHovering()
        {
            return isHov;
        }

        private static HandCursor instance;

        public static HandCursor getInstance()
        {
            if (instance == null)
            {
                instance = new HandCursor();
            }
            return instance;
        }

        public HandCursor()
        {
            instance = this;
        
            hand = new HoverButton();
            hand.ImageSource = "/Resources/Hand_Basic.png";
            hand.ActiveImageSource = hand.ImageSource;
            hand.Width = handSize;
            hand.Height = handSize;
            System.Windows.Media.Effects.DropShadowEffect effect = new System.Windows.Media.Effects.DropShadowEffect();
            effect.BlurRadius = 10;
            effect.Color = Colors.Black;
            effect.Opacity = 1;
            effect.ShadowDepth = 0;
            hand.Effect = effect;

            hand.TimeInterval = 3500;
            hand.Click += new RoutedEventHandler(cursorClicked);
        
            hand.Name = "handCursor";
            
            canvas = MainWindow.getInstance().mainCanvas;
            canvas.RegisterName(hand.Name, hand);
            canvas.Children.Add(hand);

            startAppropriateTracker();
        }

        public void startAppropriateTracker()
        {
            if (GlobalSettings.isUsingKinect)
            {
                new KinectCursorController(this);
            }
            else
            {
                new MouseCursorController(this);
            }
        }

        public double getXPosition()
        {
            return Canvas.GetLeft(hand);
        }

        public double getYPosition()
        {
            return Canvas.GetTop(hand);
        }

        public void setCursorPosition(double x, double y)
        {
            Canvas.SetLeft(hand, x - handSize / 2);
            Canvas.SetTop(hand, y - handSize);
        }

        internal System.Windows.Point getPosition()
        {
            return new System.Windows.Point(getXPosition(), getYPosition());
        }

        internal void fadeOut()
        {
            DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeOutAnimation();

                Storyboard myStoryboard = new Storyboard();
                myStoryboard.Children.Add(myDoubleAnimation);
                Storyboard.SetTargetName(myDoubleAnimation, hand.Name);
                Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

                myStoryboard.Begin(canvas);
        }

        internal void fadeIn()
        {
            DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeInAnimation();

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, hand.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

            myStoryboard.Begin(canvas);
        }
    }
}
